package com.genericdevice.platform.core.data;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xilent on 18.05.2014.
 */
public class QueryHelper {
    private static final String SELECT_CLAUSE = "SELECT";
    private static final String JOIN_CLAUSE = "JOIN";
    private static final String ON_CLAUSE = "ON";
    private static final String INSERT_CLAUSE = "INSERT";
    private static final String UPDATE_CLAUSE = "UPDATE";
    private static final String DELETE_CLAUSE = "DELETE";
    private static final String SET_CLAUSE = "SET";
    private static final String INTO_CLAUSE = "INTO";
    private static final String VALUES_CLAUSE = "VALUES";
    private static final String FROM_CLAUSE = "FROM";
    private static final String WHERE_CLAUSE = "WHERE";
    private static final String ORDERBY_CLAUSE = "ORDER BY";

    private String select;
    private List<String> join = new ArrayList<String>();
    private String from;
    private String where;
    private String order;

    private String insert;
    private List<String> values = new ArrayList<String>();

    private String update;
    private String set;

    private String delete;

    public void select(String fields) {
        select = SELECT_CLAUSE + " " + fields;
    }

    public void leftJoin(String table, String onClause) {
        join("LEFT", table, onClause);
    }

    public void rightJoin(String table, String onClause) {
        join("RIGHT", table, onClause);
    }

    public void join(String table, String onClause) {
        join(null, table, onClause);
    }

    public void join(String type, String table, String onClause) {
        join.add((type != null ? type + " " : "") + JOIN_CLAUSE + " " + table + " " + ON_CLAUSE + " " + onClause);
    }

    public void delete(String table) {
        delete = DELETE_CLAUSE + " " + FROM_CLAUSE + " " + table;
    }

    public void from(String table) {
        from = FROM_CLAUSE + " " + table;
    }

    public void where(String clauses) {
        where = WHERE_CLAUSE + " " + clauses;
    }

    public void insert(String table) {
        insert = INSERT_CLAUSE + " " + INTO_CLAUSE + " " + table;
    }

    public void values(String values) {
        this.values.add(VALUES_CLAUSE + "(" + values + ")");
    }

    public void update(String table) {
        update = UPDATE_CLAUSE + " " + table;
    }

    public void set(String values) {
        set = SET_CLAUSE + " " + values;
    }

    public void order(String fields) {
        order = ORDERBY_CLAUSE + " " + fields;
    }

    @Override
    public String toString() {
        if (select != null) {
            return buildSelectQuery();
        } else if (insert != null) {
            return buildInsertQuery();
        } else if (update != null) {
            return buildUpdateQuery();
        } else if (delete != null) {
            return buildDeleteQuery();
        }

        return null;
    }

    private String buildSelectQuery() {
        String query = select + " " + from;
        if (join != null && join.size() > 0) {
            for (String joinClause : join)
                query += " " + joinClause;
        }
        if (where != null)
            query += " " + where;
        if (order != null)
            query += " " + order;

        return query;
    }

    private String buildInsertQuery() {
        String query = insert;
        for (String value : values) {
            query += " " + value;
        }

        return query;
    }

    private String buildUpdateQuery() {
        return update + " " + set + (where != null ? " " + where : "");
    }

    private String buildDeleteQuery() {
        return delete + (where != null ? " " + where : "");
    }
}
